const app = getApp();
const api = require('../../api.js');
const util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // domain: api.xh.domain,
    isIOS: app.globalData.isIOS,
    domain: api.xh.downloadDomain,
    famous_course: [],
    teacherTagStyle: util.teacherTagStyles
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var page = this;
    var teacher_id = options.teacher_id;
    // var user_id = options.teacher_id;
    let current_user_id = wx.getStorageSync('current_user_id');

    //  教师详情
    app.sendRequest({
      url: api.teacher.student_teacher_info,
      data: {
        teacher_id: teacher_id,
        user_id: current_user_id
      },
      success: function(res) {
        if (res.data != undefined) {
          let info = res.data;
          let is_focus = info.focus;
          // 设置数据
          page.setData({
            info: info,
            is_focus: is_focus
          });
          wx.setNavigationBarTitle({
            title: '明星教师-' + info.teacher_name
          });
        } else {
          page.setData({
            info: []
          });
        }
      }
    });

    //  教师相关课程
    app.sendRequest({
      url: api.public.public_teacher_curriculum,
      data: { teacher_id: teacher_id, status: 3 },
      success: function(res) {
        if (res.data.list != undefined) {
          // 设置数据
          page.setData({
            famous_course: res.data.list
          });
        } else {
          page.setData({
            famous_course: []
          });
        }
      }
    });
  },

  toCourseDetail: function(e) {
    var id = e.currentTarget.dataset.id;
    var curriculum_name = e.currentTarget.dataset.curriculum_name;
    var url =
      '/pages/course_detail/course_detail?curriculum_id=' +
      id +
      '&curriculum_name=' +
      curriculum_name;
    // var url = '/pages/auth/auth';
    wx.navigateTo({
      url: url
    });
  },

  //  关注 & 取消关注
  toFocus: function(e) {
    var page = this;
    var content_id = e.currentTarget.dataset.id;
    var logging = wx.getStorageSync('_logining');
    if (!logging) {
      app.toAuth();
    } else {
      app.sendRequest({
        hideLoading: true,
        url: app.api.public.focus,
        data: {
          content_id: content_id,
          type: 1
        },
        method: 'POST',
        header: {
          token: wx.getStorageSync('access_token'),
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          console.log(page.data.is_focus);
          let is_focus = '';
          is_focus = page.data.is_focus == 1 ? 0 : 1;
          page.setData({
            is_focus: is_focus
          });
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
